home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 159 (1991-03-10)(Manewaldt, A.)(DE)(PD).zip / Taifun 159 (1991-03-10)(Manewaldt, A.)(DE)(PD).adf / SnoopDos / src / snoopglue.s < prev    next >
Text File  |  1998-03-07  |  6KB  |  216 lines

  1. *
  2. *    SNOOPGLUE.S                        :ts=8
  3. *
  4. *    Assembly support routines for snoopdos.c
  5. *
  6. *    This file contains routines to patch/unpatch dos library to point
  7. *    to a C routine, and to call the old routines.
  8. *
  9. *    Does some nasty tricks to patch into dos.library. However, since we
  10. *    checks to see if we are running under 2.0 (with dos.library looking
  11. *    just like any other library), Snoopdos works fine under 2.0.
  12. *
  13.     INCLUDE "exec/types.i"
  14.     INCLUDE "exec/libraries.i"
  15.     INCLUDE    "libraries/dosextens.i"
  16.  
  17.     XDEF    _installdospatch
  18.     XDEF    _uninstalldospatch
  19.     XDEF    _CallLock
  20.     XDEF    _CallOpen
  21.     XDEF    _CallLoadSeg
  22.     XDEF    _CallExecute
  23.     XDEF    _CallCurrentDir
  24.     XDEF    _CallDeleteFile
  25.  
  26.     XREF    _DOSBase
  27.  
  28.     XREF    _NewOpen
  29.     XREF    _NewLock
  30.     XREF    _NewLoadSeg
  31.     XREF    _NewExecute
  32.     XREF    _NewCurrentDir
  33.     XREF    _NewDeleteFile
  34.  
  35.     XREF    _LVOOpen
  36.     XREF    _LVOLock
  37.     XREF    _LVOLoadSeg
  38.     XREF    _LVOExecute
  39.     XREF    _LVOCurrentDir
  40.     XREF    _LVODeleteFile
  41.  
  42. CALL    MACRO
  43.     XREF    _LVO\1
  44.     JSR    _LVO\1(A6)
  45.     ENDM
  46.  
  47.     SECTION    SnoopDos,CODE
  48.  
  49. *
  50. *    Modify DOS library to call our functions for Open() and Lock();
  51. *    We can't just use SetFunction() because DOS library is non-standard.
  52. *    Instead of using JMP $123456 type instructions, it uses (mostly)
  53. *    MOVEQ #funcnum,D0; BRA.L Dispatch. So, we replace this entire
  54. *    sequence with a standard JMP instruction, steal the number from
  55. *    the MOVEQ instruction and use it when we want to call the real
  56. *    DOS library later on.
  57. *
  58. _installdospatch:
  59.     MOVEM.L    A0-A3/A6,-(A7)        * Save regs
  60.     MOVE.L    4,A6            * Get ExecBase
  61.     MOVE.L    _DOSBase,A0        * Get DOSbase
  62.     BSET    #LIBB_CHANGED,LIB_FLAGS(A0) * Indicate changing DOS library
  63.  
  64. SetFunc    MACRO
  65.     MOVEM.W    _LVO\1(A0),A1-A3    * Read in current value for func
  66.     MOVEM.W    A1-A3,\1Save        * Save the appropriate data
  67.     MOVEM.W CallOur\1,A1-A3        * Read in replacement code
  68.     MOVEM.W    A1-A3,_LVO\1(A0)    * And modify library
  69.     ENDM
  70.  
  71.     SetFunc    Open
  72.     SetFunc Lock
  73.     SetFunc LoadSeg
  74.     SetFunc Execute
  75.     SetFunc CurrentDir
  76.     SetFunc DeleteFile
  77.  
  78.     MOVE.L    A0,A1            * Finally,
  79.     CALL    SumLibrary        * Recalculate library checksum
  80.     MOVEM.L    (A7)+,A0-A3/A6        * Save regs
  81.     RTS
  82.  
  83. *
  84. *    Reinstall previous DOS patch. If someone has already altered the
  85. *    function, we can't actually exit immediately so return a FALSE
  86. *    result. While there is a very small window which could cause
  87. *    problems (if someone is in the middle of a SetFunction() when
  88. *    this is called), but we'll ignore this mostly.
  89. *
  90. _uninstalldospatch:
  91.     MOVEM.L    A0-A3/A6,-(A7)        * Save regs
  92.     MOVE.L    4,A6            * Get sysbase
  93.     MOVE.L    _DOSBase,A0        * Get DOSBase
  94.  
  95. TestFunc MACRO
  96.     MOVE.L    CallOur\1+2,D0        * Get old address of function
  97.     CMP.L    _LVO\1+2(A0),D0        * See if changed
  98.     BNE    changed            * If it has, exit with no action
  99.     ENDM
  100.  
  101.     TestFunc Open
  102.     TestFunc Lock
  103.     TestFunc LoadSeg
  104.     TestFunc Execute
  105.     TestFunc CurrentDir
  106.     TestFunc DeleteFile
  107.  
  108.     BSET    #LIBB_CHANGED,LIB_FLAGS(A0) * Indicate changing DOS library
  109.  
  110. RestFunc MACRO
  111.     MOVEM.W    \1Save,A1-A3        * Read in prev. value of vec.
  112.     MOVEM.W    A1-A3,_LVO\1(A0)    * Atomically restore it
  113.     ENDM
  114.  
  115.     RestFunc Open
  116.     RestFunc Lock
  117.     RestFunc LoadSeg
  118.     RestFunc Execute
  119.     RestFunc CurrentDir
  120.     RestFunc DeleteFile
  121.  
  122.     MOVE.L    A0,A1            * Finally,
  123.     CALL    SumLibrary        * Update library checksum
  124.     MOVEQ    #1,D0            * Indicate success
  125.     BRA.S    unexit            * And exit
  126.  
  127. changed:
  128.     MOVEQ    #0,D0            * Indicate failure to uninstall
  129. unexit:
  130.     MOVEM.L    (A7)+,A0-A3/A6        * Restore regs
  131.     RTS                * And exit with return in D0
  132.  
  133. *
  134. *    This code is the code that gets moved directly into the DOS
  135. *    library vectors
  136. *
  137. CallOurOpen:        JMP    NewOpen
  138. CallOurLock:        JMP    NewLock
  139. CallOurLoadSeg:        JMP    NewLoadSeg
  140. CallOurExecute:        JMP    NewExecute
  141. CallOurCurrentDir:    JMP    NewCurrentDir
  142. CallOurDeleteFile:    JMP    NewDeleteFile
  143.  
  144. *
  145. *    These are the replacement DOS routines.
  146. *
  147. NewFunc    MACRO
  148.     MOVEM.L    A2-A6/D2-D7,-(A7)    * Save all registers (to be safe)
  149.     JSR    _New\1            * Call C version with params in D1-D3
  150.     MOVEM.L    (A7)+,A2-A6/D2-D7    * Restore registers
  151.     MOVE.L    D0,D1            * Copy return value into D1
  152. *                    * [some routines expect this :-( ]
  153.     RTS                * Return to caller, exit value in D0
  154.     ENDM
  155.  
  156. NewOpen:    NewFunc Open
  157. NewLock:    NewFunc Lock
  158. NewLoadSeg:    NewFunc LoadSeg
  159. NewExecute:    NewFunc Execute
  160. NewCurrentDir:    NewFunc CurrentDir
  161. NewDeleteFile:    NewFunc    DeleteFile
  162.  
  163. *
  164. *    Allow's C to call the old DOS functions. If we're running Kikstart 2.0
  165. *    then just call the original function directly. Otherwise, calculate
  166. *    where to go from the code stored in the vector.
  167. *
  168. OldDosCall MACRO
  169.     LEA.L    \1Save,A0    * Get pointer to func save vector
  170.     MOVE.W    #_LVO\1,D0    * Get library offset vector
  171.     BRA.S    CallDos        * Skip to execute it
  172.     ENDM
  173.  
  174. _CallOpen:        OldDosCall Open
  175. _CallLock:        OldDosCall Lock
  176. _CallLoadSeg:        OldDosCall LoadSeg
  177. _CallExecute:        OldDosCall Execute
  178. _CallCurrentDir:    OldDosCall CurrentDir
  179. _CallDeleteFile:    OldDosCall DeleteFile
  180.  
  181.     NOP                * Stop Lattice messing up final branch
  182. CallDos:
  183.     MOVEM.L    D1-D3/D6/D7/A6,-(A7)    * Save registers
  184.     MOVE.W    (A0),D6            * Get previous instruction
  185.     CMP.W    #$4ef9,D6        * Is it a JMP instruction?
  186.     BNE    dos_exec        * If not, then call using special code
  187.     MOVE.L    2(A0),A0        * Else it's 2.0 or SetFunction()
  188.     MOVE.L    _DOSBase,A6        * Put DOSBase into A6 as expected
  189.     JSR    (A0)            * Call original function
  190.     BRA.S    callexit        * And exit
  191.  
  192. dos_exec:
  193.     MOVE.W    (A0),D6            * Get MOVEQ instruction
  194.     EXT.W    D6            * Convert data to word
  195.     EXT.L    D6            * and then longword
  196.     EXG.L    D0,D6            * Swap with library vector offset
  197.     MOVE.W    4(A0),D7        * Get offset to DOS routine from LVO
  198.     MOVE.L    _DOSBase,A6        * Get DOSBase 
  199.     ADD.W    D6,D7            * Calculate offset value
  200.     JSR    4(A6,D7.W)        * Call DOS function dispatcher
  201.  
  202. callexit:
  203.     MOVEM.L    (A7)+,D1-D3/D6/D7/A6    * Restore registers
  204.     RTS                * And return to C caller
  205.  
  206.     SECTION SnoopData,DATA
  207.  
  208. OpenSave    DS.W    3
  209. LockSave    DS.W    3
  210. LoadSegSave    DS.W    3
  211. ExecuteSave    DS.W    3
  212. CurrentDirSave    DS.W    3
  213. DeleteFileSave    DS.W    3
  214.  
  215.     END
  216.